package com.yungam.other.service.impl;

import com.yungam.common.bean.alarm_config.AlarmConfig;
import com.yungam.common.utils.HttpUtils;
import com.yungam.other.service.IotService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/* 调用 IOT 接口 */
@Service
@ConfigurationProperties(prefix = "iot")
public class IotServiceImpl implements IotService {

    private final String START_URL_SUFFIX = "/collector/v2/imcs/imcsStart";
    private final String END_URL_SUFFIX = "/collector/v2/imcs/imcsEnd";

    private String port;

    @Resource
    private HttpUtils httpUtils;

    public String getPort() { return this.port; }

    public void setPort(String port) { this.port = port; }

    @Override
    public Map<String, Object> equipScanStartAdvice(String equIp, String collectorIp) {
        String url = "http://" + collectorIp + ":" + this.port + this.START_URL_SUFFIX + "?" + "ip=" + equIp;
        httpUtils.httpExchange(url, HttpMethod.GET, null);
        return null;
    }

    @Override
    public Map<String, Object> equipScanEndAdvice(String equIp, String collectorIp) {
        String url = "http://" + collectorIp + ":" + this.port + this.END_URL_SUFFIX + "?" + "ip=" + equIp;
        httpUtils.httpExchange(url, HttpMethod.GET, null);
        return null;
    }

    @Override
    public List<Map<String, Object>> getEquipsState() {
        String url = "http://173.20.0.221:8097/api/v2/data/onlineCheck";
        Map<String, Object> map = httpUtils.httpExchange(url, HttpMethod.GET, null);
        List<Map<String, Object>> data = (List<Map<String, Object>>) map.get("data");
        return data;
    }

    @Override
    public void updateProcessThreshold(Integer equId, List<AlarmConfig> data) {
        String url = "http://192.168.100.199:8085/api/v2/equip/saveAlarmConfigsList?equipId=" + equId;
        httpUtils.httpExchange(url, HttpMethod.POST, data);
    }
}
